import { teachers, students } from './online-test'
const tokens = {
  admin: {
    token: 'admin-token'
  },
  test: {
    token: 'test-token'
  },
  teacher: {
    token: 'teacher-token'
  },
  student: {
    token: 'student-token'
  }
}

var users = {
  'admin-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Super Admin',
    college: '计算机与大数据学院',
    title: '教授',
    id: 'T08076'
  },
  'test-token': {
    roles: ['normal'],
    introduction: 'I am an tester',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Normal Tester'
  },
  'teacher-token': {
    roles: ['teacher'],
    introduction: 'I am a normal teacher',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Normal Teacher',
    college: '计算机与大数据学院',
    title: '副教授',
    id: 'T05962'
  },
  'student-token': {
    roles: ['student'],
    introduction: 'I am student',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Student',
    major: '电子信息',
    grade: '2021级',
    college: '计算机与大数据学院',
    id: '221701336'
  }
}

export default [
  // user login
  {
    url: '/auth/login',
    type: 'post',
    response: config => {
      const { username, password } = config.body
      var userName = ''
      console.log(username)
      console.log(password)
      if (students[username] && students[username].password == password) {
        userName = 'student'
        users['student-token'].name = students[username].name
        users['student-token'].id = students[username].s_id
        users['student-token'].college = students[username].college
        users['student-token'].grade = students[username].grade
        users['student-token'].major = students[username].major
      } else if (teachers[username] && teachers[username].password == password) {
        if(teachers[username].admin == '0'){
          userName = 'teacher'
          users['teacher-token'].name = teachers[username].name
          users['teacher-token'].id = teachers[username].id
          users['teacher-token'].college = teachers[username].college
          users['teacher-token'].title = teachers[username].title
        } else {
          userName = 'admin'
          users['admin-token'].name = teachers[username].name
          users['admin-token'].id = teachers[username].id
          users['admin-token'].college = teachers[username].college
          users['admin-token'].title = teachers[username].title
        }
        
      }
      const token = tokens[userName]
      // mock error
      if (!token) {
        return {
          code: 401,
          message: 'Account and password are incorrect.'
        }
      }
      const result = {
        'token': token.token,
        'tokenHead': 'Bearer'
      }

      return result
    }
  },

  // get user info
  {
    url: '/auth/info\.*',
    type: 'get',
    response: config => {
      var { token } = config.query
      token = token.substr(6)
      const info = users[token]

      // mock error
      if (!info) {
        return {
          code: 401,
          message: 'Login failed, unable to get user details.'
        }
      }

      return info
    }
  },

  // user logout
  {
    url: '/auth/logout',
    type: 'post',
    response: _ => {
      return {
        code: 200
      }
    }
  }
]
